import axios from 'axios'
import type { AxiosInstance } from 'axios'
import type { RequestInterceptors, RequestConfig } from './type'
import { ElLoading } from 'element-plus'
import { LoadingInstance } from 'element-plus/lib/components/loading/src/loading'

const DEFAULT_LOADING = true

class Request {
  private instance: AxiosInstance
  inerceptors?: RequestInterceptors
  showLoading: boolean
  loading?: LoadingInstance
  // RequestConfig 重写config类型参数
  constructor(config: RequestConfig) {
    // 创建axios实例
    this.instance = axios.create(config)
    this.inerceptors = config.inerceptors
    /* 不同请求可选是否显示loading 默认显示 */
    /* eg: a.b ?? [] ==> a.b ? a.b : [] */
    this.showLoading = config.showLoading ?? DEFAULT_LOADING
    // 单个对应实例请求拦截器
    this.instance.interceptors.request.use(
      this.inerceptors?.requestInterceptor,
      this.inerceptors?.requestErrorInterceptor
    )
    // 单个对应实例响应拦截器
    this.instance.interceptors.response.use(
      this.inerceptors?.responseInterceptor,
      this.inerceptors?.responseErrorInterceptor
    )
    // 全局实例添加请求拦截器
    this.instance.interceptors.request.use(
      (config) => {
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            fullscreen: true,
            text: '加载中...'
            // background: 'rgba(0,0,0,0.5)'
          })
        }
        return config
      },
      (error) => {
        console.log('global request error！', error)
        return Promise.reject(error)
      }
    )
    // 全局实例添加响应拦截器
    this.instance.interceptors.response.use(
      (res) => {
        // 移除loading效果
        this.loading?.close()
        // 拦截响应的数据
        const { data } = res
        if (data) {
          return data.data
        } else {
        }
      },
      (error) => {
        console.log('global response error！')
        console.log(error)
        return Promise.reject(error)
      }
    )
  }
  // 请求方法
  request<T>(config: RequestConfig<T>): Promise<T> {
    // 请求配置中不显示loading
    if (config.showLoading === false) {
      this.showLoading = config.showLoading
    }
    return new Promise((resolve, reject) => {
      // 单个请求添加拦截
      if (config.inerceptors?.requestInterceptor) {
        config = config.inerceptors.requestInterceptor(config)
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          /* res 类型限制 <any, T> */
          //单个响应添加拦截
          if (config.inerceptors?.responseInterceptor) {
            res = config.inerceptors.responseInterceptor(res)
          }
          // 重新设置为显示loading 不会影响下一次请求
          this.showLoading = DEFAULT_LOADING
          resolve(res)
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING
          reject(err)
        })
    })
  }
  get<T = any>(config: RequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'GET' })
  }
  post<T = any>(config: RequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'POST' })
  }
  delete<T = any>(config: RequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'DELETE' })
  }
  patch<T = any>(config: RequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'PATCH' })
  }
}

export default Request
